<?php
declare (strict_types=1);

namespace app\exam\model\admin;

use app\common\model\BaseModel;
use think\model\concern\SoftDelete;
use app\common\logic\category\Category;

/**
 * Class Paper
 *
 * @package app\exam\model\admin
 */
class Paper extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'exam_paper';

    protected $type = [
        'start_time'       => 'timestamp',
        'end_time'         => 'timestamp',
        'fullcategorypath' => 'array'
    ];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/21
     *
     * @param $query
     */
    public function scopeMhm($query)
    {
        // 机构
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 试卷类型
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/21
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getPaperTypeTextAttr($value, $data)
    {
        $text = [
            1 => '固定卷',
            2 => '随机卷'
        ];
        return $text[$data['paper_type']] ?? '未知';
    }

    /**
     * 获取试卷类型选项
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/4
     *
     * @return array[]
     */
    public function getPaperTypeOptions()
    {
        return [
            ['title' => '不限制', 'value' => -1],
            ['title' => '固定卷', 'value' => 1],
            ['title' => '随机卷', 'value' => 2],
        ];
    }

    /**
     * 作答时长
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/21
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getTotalTimeTextAttr($value, $data)
    {
        return $data['total_time'] == 0 ? '不限制' : $data['total_time'] . '分钟';
    }

    /**
     * 考试次数
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/21
     *
     * @param $value
     * @param $data
     * @return string
     */
    public function getExamNumberTextAttr($value, $data)
    {
        return $data['exam_number'] == 0 ? '不限制' : $data['exam_number'] . '次';
    }

    /**
     * 获取分类全路径名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-17
     * @param  [type] $v    [description]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function getCategoryStrAttr($v, $data)
    {
        return $data['category'] ? app(Category::class)->parseTitle($data['category'], 3) : '';
    }

    /**
     * 新增数据事件
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/21
     *
     * @static
     * @param \think\Model $paper
     * @return mixed|void
     */
    public static function onBeforeInsert($paper)
    {
        // 机构
        $paper->mhm_id = $paper->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 关联部分数据
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2020/12/22
     *
     * @return \think\model\relation\HasMany
     */
    public function paperParts()
    {
        return $this->hasMany(PaperParts::class, 'paper_id', 'id');
    }

    /**
     * 关联套卷选中
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-16
     * @return [type] [description]
     */
    public function checked()
    {
        return $this->hasOne(ModuleRollupPaper::class, 'paper_id');
    }
}